package com.SmartCampus.system.controller;

import com.SmartCampus.system.annotation.AuthCheck;
import com.SmartCampus.system.common.BaseResponse;
import com.SmartCampus.system.common.DeleteRequest;
import com.SmartCampus.system.common.ErrorCode;
import com.SmartCampus.system.common.ResultUtils;
import com.SmartCampus.system.constant.UserConstant;
import com.SmartCampus.system.exception.BusinessException;
import com.SmartCampus.system.exception.ThrowUtils;
import com.SmartCampus.system.model.dto.subjectcategory.SubjectAddRequest;
import com.SmartCampus.system.model.dto.subjectcategory.SubjectUpdateRequest;
import com.SmartCampus.system.model.entity.Subjectcategory;
import com.SmartCampus.system.service.SubjectcategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 学科分类接口
 *
 * @author wangliang
 */
@RestController
@RequestMapping("/subject")
@Slf4j
@Api(tags = "学科分类接口")
public class SubjectCategoryController {

    @Resource
    private SubjectcategoryService subjectcategoryService;

    // region 增删改查

    /**
     * 创建学科（仅管理员）
     *
     * @param subjectAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "创建学科（仅管理员）")
    public BaseResponse<Long> addUser(@RequestBody SubjectAddRequest subjectAddRequest) {
        if (subjectAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Subjectcategory> querySubject = new QueryWrapper<>();
        querySubject.eq("subjectName", subjectAddRequest.getSubjectName());
        if (subjectcategoryService.count(querySubject) > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "学科已存在");
        }
        Subjectcategory subjectcategory = new Subjectcategory();
        BeanUtils.copyProperties(subjectAddRequest, subjectcategory);
        boolean result = subjectcategoryService.save(subjectcategory);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(subjectcategory.getSubjectId());
    }

    /**
     * 删除学科（仅管理员）
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除学科（仅管理员）")
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = subjectcategoryService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * 更新学科（仅管理员）
     *
     * @param subjectUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "更新学科（仅管理员）")
    public BaseResponse<Boolean> updateUser(@RequestBody SubjectUpdateRequest subjectUpdateRequest) {
        if (subjectUpdateRequest.getSubjectId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Subjectcategory subjectcategory = new Subjectcategory();
        BeanUtils.copyProperties(subjectUpdateRequest, subjectcategory);
        subjectcategory.setSubjectId(subjectUpdateRequest.getSubjectId());
        boolean result = subjectcategoryService.updateById(subjectcategory);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * 获取全部学科
     *
     * @return
     */
    @GetMapping("/list/all")
    @ApiOperation(value = "获取全部学科")
    public BaseResponse<List<Subjectcategory>> listUserByPage() {
        QueryWrapper<Subjectcategory> queryWrapper = new QueryWrapper<>();
        List<Subjectcategory> subjectcategoryList = subjectcategoryService.list(queryWrapper);
        return ResultUtils.success(subjectcategoryList);
    }

    // endregion
}
